package com.sls.game.service.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.NutsNetGoodsInfoDAO;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.service.NutsNetGoodsUpdateService;

/**
 * @Description:商品下架与修改价格
 * @author Wangxuegang
 * @time 2012-08-30
 * 
 */
@Service("nutsNetGoodsUpdateService")
public class NutsNetGoodsUpdateServiceImpl implements NutsNetGoodsUpdateService {

	private static Logger log = Logger
			.getLogger(NutsNetGoodsAddServiceImpl.class);

	/**
	 * 坚果网络商品DAO
	 */
	private NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO;

	private Gson gson = null;

	public NutsNetGoodsUpdateServiceImpl() {
		gson = CommonUtil.gson();
	}

	public NutsNetGoodsInfoDAO getNutsNetGoodsInfoDAO() {
		return nutsNetGoodsInfoDAO;
	}

	@Resource(name = "nutsNetGoodsInfoDAO")
	public void setNutsNetGoodsInfoDAO(NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO) {
		this.nutsNetGoodsInfoDAO = nutsNetGoodsInfoDAO;
	}

	/**
	 * 坚果网络商品货源变更
	 * 
	 * @param body
	 *            请求报文body
	 * @param reqId
	 *            请求ID
	 * @throws ServerDisponseException
	 */
	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		NutsNetGoodsInfo nutsNetGoodsInfo = gson.fromJson(body,
				NutsNetGoodsInfo.class);
		String partnerId = nutsNetGoodsInfo.getPartnerId();
		String serialNumber = nutsNetGoodsInfo.getSerialNumber();
		String supProductId = nutsNetGoodsInfo.getSupProductId();
		String opCode = nutsNetGoodsInfo.getOpCode();
		String unitPrice = nutsNetGoodsInfo.getUnitPrice();
		String ts = nutsNetGoodsInfo.getTs();
		String sign = nutsNetGoodsInfo.getSign();

		boolean flag = checkData(partnerId, serialNumber, supProductId, opCode,
				ts, sign, head.getRequestId());
		if (flag) {
			NutsNetGoodsInfo nutsNetGoodsInfo1 = null;
			NutsNetGoodsInfo nutsNetGoods = new NutsNetGoodsInfo();
			nutsNetGoodsInfo1 = nutsNetGoodsInfoDAO.queryById(supProductId);
			if (nutsNetGoodsInfo1 != null) {
				if ("DELETE".equals(opCode)) { // 删除货源
					log.info("坚果网络删除商品编号为" + supProductId + "的货源");
					nutsNetGoods.setId(nutsNetGoodsInfo1.getId());
					nutsNetGoods.setPartnerId(partnerId);
					nutsNetGoods.setSupProductId(supProductId);
					nutsNetGoods.setUnitPrice(nutsNetGoodsInfo1.getUnitPrice()); // 删除货源，坚果不传递该参数
					nutsNetGoods.setName(nutsNetGoodsInfo1.getName());
					nutsNetGoods.setProductType(nutsNetGoodsInfo1.getProductType());
					nutsNetGoods.setParvalue(nutsNetGoodsInfo1.getParvalue());
					nutsNetGoods.setTs(ts);
					nutsNetGoods.setSign(sign);
					nutsNetGoods.setSerialNumber(serialNumber);
					nutsNetGoods.setOpCode(opCode);
					nutsNetGoods.setGoodsDelete("0"); // "0":该商品删除货源;"1":该商品正常供货
					nutsNetGoods.setIsAudit("0"); // 0：未审核，1已审核
					try {
						nutsNetGoodsInfoDAO.update(nutsNetGoods);

						if (log.isInfoEnabled()) {
							log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
						}

						return null;
					} catch (Exception e) {
						log.info("Service处理[" + head.getRequestId()
								+ "]的请求出现数据更新异常！");
						throw new ServerDisponseException(
								ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
								ExceptionCodeDes.ACCESS_DATABASE_ERROR
										.getDescription());
					}

				} else if ("UPDATE".equals(opCode)) { // 更改货源价格
					if (log.isInfoEnabled()) {
						log.info("坚果网络更改商品编号为" + supProductId + "的货源价格");
					}

					nutsNetGoods.setId(nutsNetGoodsInfo1.getId());
					nutsNetGoods.setPartnerId(partnerId);
					nutsNetGoods.setSupProductId(supProductId);
					if ("".equals(unitPrice) || unitPrice == null) {
						throw new ServerDisponseException(
								ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL.getCode(),
								ExceptionCodeDes.GOODS_SUPPLY_PRICE_NULL
										.getDescription());
					} else {
						nutsNetGoods.setUnitPrice(unitPrice); // 货源更改价格，该参数不能为空
					}

					nutsNetGoods.setName(nutsNetGoodsInfo1.getName());
					nutsNetGoods.setProductType(nutsNetGoodsInfo1.getProductType());
					nutsNetGoods.setParvalue(nutsNetGoodsInfo1.getParvalue());
					nutsNetGoods.setTs(ts);
					nutsNetGoods.setSign(sign);
					nutsNetGoods.setSerialNumber(serialNumber);
					nutsNetGoods.setOpCode(opCode);
					nutsNetGoods.setGoodsDelete("1"); // "0":该商品删除货源;"1":该商品正常供货
					nutsNetGoods.setIsAudit("0"); // 0：未审核，1已审核
					try {
						nutsNetGoodsInfoDAO.update(nutsNetGoods);
						if (log.isInfoEnabled()) {
							log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
						}

						return null;
					} catch (Exception e) {
						log.info("Service处理[" + head.getRequestId()
								+ "]的请求出现数据更新异常！");
						throw new ServerDisponseException(
								ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
								ExceptionCodeDes.ACCESS_DATABASE_ERROR
										.getDescription());
					}
				}
			}
		}
		return null;
	}

	/**
	 * 对相关数据进行校验
	 * 
	 * @param partnerId
	 * @param serialNumber
	 * @param supProductId
	 * @param opCode
	 * @param ts
	 * @param sign
	 * @param reqId
	 * @return
	 * @throws ServerDisponseException
	 */
	private boolean checkData(String partnerId, String serialNumber,
			String supProductId, String opCode, String ts, String sign,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		// 合作平台编号
		if ("".equals(partnerId) || partnerId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL
							.getDescription());
		}
		// 货源发布序列号
		else if ("".equals(serialNumber) || serialNumber == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getCode(),
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getDescription());
		}
		// 商品中心商品编号
		else if ("".equals(supProductId) || supProductId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		}
		// 操作类型
		else if ("".equals(opCode) || opCode == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATE_TYPE_NULL.getCode(),
					ExceptionCodeDes.OPERATE_TYPE_NULL.getDescription());
		}
		// 时间戳
		else if ("".equals(ts) || ts == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TIME_STAMP_NULL.getCode(),
					ExceptionCodeDes.TIME_STAMP_NULL.getDescription());
		}
		// 签名字符串
		else if ("".equals(sign) || sign == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SIG_NATURE_NULL.getCode(),
					ExceptionCodeDes.SIG_NATURE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}
}
